const util = require("../../utils/util")
const api = require("../../request/api")
import Toast from '../../vant/weapp/toast/toast';
import Dialog from '../../vant/weapp/dialog/dialog';


// pages/order/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myOrderList:[],
    orderList:[],
    myActiveNames: '1',
    joinActiveNames: '1',
    showPopup:false,
    popup:'',
    showShade:false,
    isMyOrder:true,
    value:'',
  },
  onClick(){
    var value = this.data.value;
    if(value == '' || value == null ){
      return this.getMyOrder();
    }
    util.post(api.order.qryKeyword,{createManId:wx.getStorageSync('userInfo').userId,keyword:this.data.value,orderBy:'createTime'})
    .then(res => {
      if(res.code == 200){
        this.setData({
          myOrderList:res.data.list
        })
      }else{
        Toast.fail(res.message);
      }
    })
  },
  getMonet(e){
    this.setData({ 
      showPopup: false
    });
    var detail = e.currentTarget.dataset.variable;
    console.log(detail);
    wx.navigateTo({
      url: `../payPage/payPage?orderId=${detail.orderId}&payStatus=${detail.payStatus}`
    })
  },
  saveMonet(e){
    this.setData({ 
      showPopup: false
    });
    var detail = e.currentTarget.dataset.variable;
    console.log(detail);
    wx.navigateTo({
      url: `../payPage/payPage?orderId=${detail.orderId}&payStatus=${detail.payStatus}`
    })
  },

  checkIn(e){
    Dialog.confirm({
      title: '消息提示',
      message: '确认签到吗？',
    })
    .then(() => {
      // on confirm
      this.checkIn_0(e)
    })
    .catch(() => {
      // on cancel
    });
  },
  checkIn_0(e){
    this.setData({ showShade: true });
    Toast.loading({
      duration: 0,
      message: '签到中...',
      forbidClick: true,
    });
    var detail = e.currentTarget.dataset.variable;
    util.post(api.confirm.upStatus,{orderId:detail.orderId,status:2}).then(res => {
      this.setData({ showShade: false });
      Toast.clear();
      if(res.code == 200){
        Toast.success("签到成功");
      }else{
        Toast.fail(res.message);
      }
      this.getMyOrder();
      this.getJoinOrder();
    }).catch(() => {
      this.setData({ showShade: false });
      Toast.clear();
    })
  },
  endOrderPin(e){
    Dialog.confirm({
      title: '消息提示',
      message: '确认结束拼单吗？',
    })
    .then(() => {
      // on confirm
      this.endOrderPin_0(e)
    })
    .catch(() => {
      // on cancel
    });
  },
  endOrderPin_0(e){
    this.setData({ showShade: true });
    Toast.loading({
      duration: 0,
      message: '结束拼单中...',
      forbidClick: true,
    });
    var detail = e.currentTarget.dataset.variable;
    util.post(api.order.upStatus,{orderId:detail.orderId,orderStatus:2}).then(res => {
      this.setData({ showShade: false });
      Toast.clear();
      if(res.code == 200){
        Toast.success("结束拼单成功");
      }else{
        Toast.fail(res.message);
      }
      this.getMyOrder();
      this.getJoinOrder();
    }).catch(() => {
      this.setData({ showShade: false });
      Toast.clear();
    })
  },
  
  outOrder(e){
    Dialog.confirm({
      title: '消息提示',
      message: '这项操作可能对您的信用造成影响，确认退出吗？',
    })
    .then(() => {
      // on confirm
      this.outOrder_0(e)
    })
    .catch(() => {
      // on cancel
    });
  },
  outOrder_0(e){
    this.setData({ showShade: true });
    Toast.loading({
      duration: 0,
      message: '取消订单中...',
      forbidClick: true,
    });
    var detail = e.currentTarget.dataset.variable;
    util.post(api.order.upStatus,{orderId:detail.orderId,orderStatus:4}).then(res => {
      this.setData({ showShade: false });
      Toast.clear();
      if(res.code == 200){
        Toast.success("取消订单成功");
      }else{
        Toast.fail(res.message);
      }
      this.getMyOrder();
      this.getJoinOrder();
    }).catch(() => {
      this.setData({ showShade: false });
      Toast.clear();
    })
  },
  
  cancelOrder(e){
    Dialog.confirm({
      title: '消息提示',
      message: '这项操作可能对您的信用造成影响，确认取消订单吗？',
    })
    .then(() => {
      // on confirm
      this.cancelOrder_0(e)
    })
    .catch(() => {
      // on cancel
    });
  },
  cancelOrder_0(e){
    this.setData({ showShade: true });
    Toast.loading({
      duration: 0,
      message: '取消订单中...',
      forbidClick: true,
    });
    var detail = e.currentTarget.dataset.variable;
    util.post(api.order.upStatus,{orderId:detail.orderId,orderStatus:4}).then(res => {
      this.setData({ showShade: false });
      Toast.clear();
      if(res.code == 200){
        Toast.success("取消订单成功");
      }else{
        Toast.fail(res.message);
      }
      this.getMyOrder();
      this.getJoinOrder();
    }).catch(() => {
      this.setData({ showShade: false });
      Toast.clear();
    })
  },
  
  endOrder(e){
    Dialog.confirm({
      title: '消息提示',
      message: '确认完成订单吗？',
    })
    .then(() => {
      // on confirm
      this.endOrder_0(e)
    })
    .catch(() => {
      // on cancel
    });
  },
  endOrder_0(e){
    this.setData({ showShade: true });
    Toast.loading({
      duration: 0,
      message: '结束订单中...',
      forbidClick: true,
    });
    var detail = e.currentTarget.dataset.variable;
    util.post(api.order.upStatus,{orderId:detail.orderId,orderStatus:3}).then(res => {
      this.setData({ showShade: false });
      Toast.clear();
      if(res.code == 200){
        Toast.success("结束订单成功");
      }else{
        Toast.fail(res.message);
      }
      this.getMyOrder();
    }).catch(() => {
      this.setData({ showShade: false });
      Toast.clear();
    })
  },
  getMyOrder(){
    this.setData({ showShade: true });
    Toast.loading({
      duration: 0,
      message: '加载中...',
      forbidClick: true,
    });
    util.post(api.order.query,{queryObj:{createManId:wx.getStorageSync('userInfo').userId,orderBy:'createTime'}}).then(res =>{
      Toast.clear();
      this.setData({ showShade: false });
      if(res.code == 200){
        this.setData({
          myOrderList:res.data.list
        })
      }else{
        Toast.fail(res.message);
      }
    }).catch(() => {
      Toast.clear();
      this.setData({ showShade: false });
    })
  },
  getJoinOrder(){
    this.setData({ showShade: true });
    Toast.loading({
      duration: 0,
      message: '加载中...',
      forbidClick: true,
    });
    util.post(api.order.joinOrder,{queryObj:{userId:wx.getStorageSync('userInfo').userId}}).then(res =>{
      Toast.clear();
      this.setData({ showShade: false });
      if(res.code == 200){
        this.setData({
          orderList:res.data.list
        })
      }else{
        Toast.fail(res.message);
      }
    }).catch(() => {
      Toast.clear();
      this.setData({ showShade: false });
    })
  },
  getOrderDetail(e){
    var detail = e.currentTarget.dataset.variable;
    this.setData({ 
      showPopup: true,
      popup:detail 
    });
  },
  popupClose() {
    this.setData({ showPopup: false });
  },
  myCollapseChange(event) {
    console.log(event.detail)
    this.setData({
      myActiveNames: event.detail,
    });
  },
  joinCollapseChange(event) {
    console.log(event.detail)
    this.setData({
      joinActiveNames: event.detail,
    });
  },
  tabsChange(){
    this.setData({
      isMyOrder:!this.data.isMyOrder
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var a = wx.getStorageSync('hasUserInfo');
    if( a != null && a != '' && a == true){
      this.getMyOrder();
      this.getJoinOrder();
    }else{
      this.setData({
        myOrderList:[],
        orderList:[],
        activeName: '1',
        showPopup:false,
        popup:'',
        showShade:false,
      })
    }
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
      this.setData({
        showPopup:false,
        popup:'',
        showShade:false,
        isMyOrder:true,
        value:'',
      })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})